package edu.ads.bookloan

class Person {

	transient springSecurityService

	String username
	String password
	boolean enabled = true
	boolean accountExpired = false
	boolean accountLocked = false
	boolean passwordExpired = false
	Degree degree
	
	static belongsTo = [Degree]
	
	static constraints = {
		username blank: false, unique: true
		password blank: false
		degree nullable: true
	}

	static mapping = {
		password column: '`password`'
	}

	Set<Authority> getAuthorities() {
		PersonAuthority.findAllByPerson(this).collect { it.authority } as Set
	}

	def beforeInsert() {
//		encodePassword()
	}

	def beforeUpdate() {
//		if (isDirty('password')) {
//			encodePassword()
//		}
	}

	protected void encodePassword() {
		password = springSecurityService.encodePassword(password)
	}
	
	String toString(){
		username
	}
}
